package com.istock.union.user.service.impl;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.istock.base.common.api.model.PageInfo;
import com.istock.base.mybatis.utils.MybatisFieldConvertUtils;
import com.istock.union.user.dao.DeptInfoMapper;
import com.istock.union.user.dao.LoginforInfoMapper;
import com.istock.union.user.model.LoginLogQuery;
import com.istock.union.user.model.LoginforInfo;
import com.istock.union.user.model.LoginforInfoExample;
import com.istock.union.user.service.LoginforInfoService;

@Repository
public class LoginforInfoServiceImpl implements LoginforInfoService{

	@Autowired
	private LoginforInfoMapper mapper;
	@Autowired
	private DeptInfoMapper deptInfoMapper;
	
	@Autowired
	private SqlSessionFactoryBean sessionFactory;
	
	public void insertLoginfor(LoginforInfo loginforInfo){
		
		mapper.insertSelective(loginforInfo);
	}
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	public List search(LoginLogQuery query, PageInfo page) {
		if (query != null) {
			LoginforInfoExample example = new LoginforInfoExample();
			LoginforInfoExample.Criteria criteria = example.createCriteria();

			if (StringUtils.isNotBlank(query.getBeginTime())) {
				Date beginTime;
				try {
					beginTime = DateUtils.parseDate(query.getBeginTime(), "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss");
					criteria.andOperTimeGreaterThanOrEqualTo(beginTime);
				} catch (ParseException e) {
					logger.error("can't parser the date for beginTime:[{}]", query.getBeginTime());
				}
			}

			if (StringUtils.isNotBlank(query.getEndTime())) {
				Date beginTime;
				try {
					beginTime = DateUtils.parseDate(query.getEndTime(), "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss");
					criteria.andOperTimeLessThan(beginTime);
				} catch (ParseException e) {
					logger.error("can't parser the date for endTime:[{}]", query.getEndTime());
				}
			}

			if (StringUtils.isNotBlank(query.getIpaddr())) {
				criteria.andIpaddrLike("%" + query.getIpaddr() + "%");
			}
			if (StringUtils.isNotBlank(query.getLoginName())) {
				criteria.andLoginNameLike("%" + query.getLoginName() + "%");
			}
			if(StringUtils.isNotBlank(query.getOrderByColumn())) {
				example.setOrderByClause(MybatisFieldConvertUtils.getInstance().unmarshal("com.istock.union.user.dao.LoginforInfoMapper", "BaseResultMap", query.getOrderByColumn())+" "+query.getIsAsc());
			}else {
				example.setOrderByClause("OPER_TIME DESC");
			}
			
			mapper.selectByExample(example, page);
			
//			List<Map> list = DataTranslateUtils.translate(page.getData(), "deptCode", "deptName", new DeptTranslateCallback());
//			page.setData(list);
			return page.getData();
		}
		return null;
	}

	public void deleteByIds(String ids) {
		LoginforInfoExample example = new LoginforInfoExample();
		LoginforInfoExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(Arrays.asList(ids));
		mapper.deleteByExample(example);
	}

	@Override
	@Transactional
	public void insertLoginBatch(List<LoginforInfo> loginforList) throws RuntimeException{
		try {
			SqlSession session = sessionFactory.getObject().openSession(ExecutorType.BATCH);
			for(LoginforInfo info : loginforList) {
				info.setOperTime(new Date());
				mapper.insertSelective(info);
			}
			session.flushStatements();
			
		} catch (Exception e) {
			logger.error("insertLoginBatch exception " , e);
		}
		
	}

}
